package com.cqust.wdm.emsboot.controller;


import com.cqust.wdm.emsboot.config.security.ShiroUtils;
import com.cqust.wdm.emsboot.domain.*;
import com.cqust.wdm.emsboot.service.IOrderService;
import com.cqust.wdm.emsboot.service.IProductService;
import com.cqust.wdm.emsboot.util.AjaxResult;
import com.cqust.wdm.emsboot.util.BaseController;
import com.cqust.wdm.emsboot.util.ExcelUtil;
import com.cqust.wdm.emsboot.util.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 订单信息Controller
 */
@RestController
@RequestMapping("/emsboot/order")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IProductService productService;

    /**
     * app查询订单信息列表
     */
    @ApiOperation("app信息列表")
    @GetMapping("/app")
    public TableDataInfo app(Order order) {
        order.setState(1);
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 客户报表
     */
    @GetMapping("/selectSumCountGroupByUid")
    public TableDataInfo selectSumCountGroupByUid(Order order) {
        startPage();
        List<CustomerTO> list = orderService.selectSumCountGroupByUid(order);
        return getDataTable(list);
    }


    /**
     * 查询信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        User userInfo = ShiroUtils.getUserInfo();

        if (userInfo != null) {
            //普通用户查询 自己的入库凭证、备货通知单
            if ("普通用户".equals(userInfo.getRoleName()))
                order.setCid(String.valueOf(userInfo.getId()));
        }
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<OrderExcel> util = new ExcelUtil<>(OrderExcel.class);
        List<OrderExcel> excelList = util.importExcel(file.getInputStream());

        String message = orderService.importOrder(excelList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 查询订单信息列表
     */
    @GetMapping("/export")
    public AjaxResult export(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 查询订单信息列表
     */
    @GetMapping("/exportout")
    public AjaxResult exportout(Order order) {
        startPage();
        List<OrderOut> list = orderService.selectOrderOutList(order);
        ExcelUtil<OrderOut> util = new ExcelUtil<OrderOut>(OrderOut.class);
        return util.exportExcel(list, "入库信息");
    }


    /**
     * 获取订单信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(orderService.selectOrderById(id));
    }

    /**
     *新增入库凭证/备货通知单
     */
    @ApiOperation("新增入库凭证/备货通知单")
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody OrderAddDT orderAddDT) {
        System.out.println(orderAddDT);
        Order order = new Order();
        try {
            int pid = Integer.parseInt(orderAddDT.getPid());
            order.setPid(String.valueOf(orderAddDT.getPid()));
        } catch (Exception e) {
            //新增商品
            Product product = new Product();
            product.setName(orderAddDT.getPid());//新增的商品名称
            product.setCategoryId(orderAddDT.getCategoryId());
            product.setMinimum(orderAddDT.getMinimum());
            //新增商品
            productService.insertProduct(product);

            order.setPid(String.valueOf(product.getId()));

        }
        User userInfo = ShiroUtils.getUserInfo();
        order.setCid(String.valueOf(userInfo.getId()));
        order.setCount(orderAddDT.getCount());
        order.setCertificateId(orderAddDT.getCertificateId());
        order.setPackaging(orderAddDT.getPackaging());
        order.setSpecification(orderAddDT.getSpecification());
        order.setRemark(orderAddDT.getRemark());
        order.setState(orderAddDT.getState());
        order.setExpressName(orderAddDT.getExpressName());
        order.setExpressCode(orderAddDT.getExpressCode());

        return AjaxResult.success(orderService.insertOrder(order));


    }

    /**
     * 修改订单信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    @ApiOperation("删除订单信息")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }
}
